package com.sg.hms.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.sg.hms.Model.Employee2;
import com.sg.hms.Service.SalaryService;
import com.sg.hms.ServiceImpl.SalaryServiceImpl;
/**
 * 
 * @author lubinghan
 *
 */
public class ShowDepartPayrolSpendContraller extends HttpServlet{
	SalaryService sService = new SalaryServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.setCharacterEncoding("utf-8");
		req.setCharacterEncoding("utf-8");
		PrintWriter pw = resp.getWriter();
		//获取请求参数
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		int pageStart = (page-1)*pageSize;
		String dateStr = req.getParameter("date");
		if(dateStr==null){//加载时日期为空则设为当前日
			Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH)+1;
			int date = c.get(Calendar.DATE); 
			dateStr = year + "-" + month + "-" + date ;
		}
		//截取日期
		String datestr1 = dateStr.substring(0, 4);
		String dateStr2 = dateStr.substring(5,7);
		String date = datestr1+dateStr2;
		List<Employee2> employees = sService.selectDepartSalarySpendByDate(date, pageStart, pageSize);//部门员工工资和
		JSONObject DepartSalarySpend = new JSONObject();
		DepartSalarySpend.put("total", sService.selectDepartSalaryDataTotalByDate(date));
		DepartSalarySpend.put("rows", employees);
		pw.write(DepartSalarySpend.toString());
	}
}
